const app = getApp()
var http = require("../../utils/httpUtils")
var common = require("../../utils/common")

Page({
	data: {
		maskStatus: true,
		animations: false,
		state: '',
		//tab列表
		menuIndex: 0,
		//机构数据
		organizationData: {},
		//首页特惠数据
		organizationDiscountData: {},
		//广告板块数据
		bannerData: [],
		//特色课程数据
		courseData: [],
		//机构活动数据
		activityData: [],
		commontData: {},
		//机构服务菜单默认页索引
		organizationServiceContentItemIndex: 0,
		//菜单选中视图X坐标
		menuItemSelectedX: 0,
		//菜单内容容器宽度
		menuItemContentW: 0,
		//菜单宽度
		menuItemW: 0,
		//记录菜单最后一次平移完成后的位置
		lastMenuItemSelectedX: 0,
		//菜单内容容器高度
		menuItemContentH: 0,
		//机构概况内容填充完成后高度
		organizationDescH: 0,
		//特色课程内容填充完成后的高度
		organizationCourseH: 0,
		//机构活动填充完成后的高
		organizationActivityH: 0,
		//机构评价填充完成后高
		organizationCommentH: 0,
		//模态框显示
		diaLogIsShow: false,
	},
	// 跳转地图
	goMap: function (e) {
		let _this = this;
		let latitude = e.currentTarget.dataset.lat //纬度
		let longitude = e.currentTarget.dataset.long //经度
		let storeName = e.currentTarget.dataset.name //机构名
		let address = e.currentTarget.dataset.add //机构地址
		//将百度地图的坐标转为腾讯地图的坐标
		let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
		let x = longitude - 0.0065; //经度
		let y = latitude - 0.006; //纬度
		let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
		let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
		let lngs = z * Math.cos(theta);
		let lats = z * Math.sin(theta);

		wx.openLocation({
			latitude: lats, //纬度
			longitude: lngs, //经度
			name: storeName,
			address: address
		})
	},
	// 拨打机构电话
	freeTell: function () {
		wx.makePhoneCall({
			phoneNumber: '18591951503',
		})
	},
	// 显示预约框
	diaLogShow: function (e) {
		this.setData({
			diaLogIsShow: true
		})
	},
	// 提交预约
	onDialogConfirm: function (e) {
		console.log(e.detail.value)
		if (e.detail.value.phone == '') {
			wx.showToast({
				title: '请填写联系人',
				icon: 'none',
				duration: 2000
			})
			return
		}

		var urls = app.globalData.SERVER_IP + "/user/addAboutCourse"
		var params = {
			"ar_uid": wx.getStorageSync('userInfo').userid,
			"openid": "xxxx",
			"ar_name": e.detail.value.contact || '匿名',
			"ar_phone": e.detail.value.phone,
			"ar_des": e.detail.value.note || '无备注',
			"ar_cid": 0,
			"ar_cname": "暂无名称",
			"o_name": this.data.organizationData.o_name,
			"ar_oid": this.data.organizationData.o_id,
			"ar_ophone": this.data.organizationData.o_phone,
			"clickurl": ""
		}
		http.P(urls, params, (res) => {
			console.log(res.success)
			if (res.success) {
				wx.showToast({
					title: '预约成功',
					icon: 'success',
					duration: 2000
				})
				this.setData({
					diaLogIsShow: false
				})
			}
		}, (err) => {
			console.log("获取首页数据失败")
			console.log(err)
		})
	},
	// 授权加跳转
	getUserInfo: function (e) {
		if (e.detail.userInfo) {
			console.log("----授权成功")
			console.log(e.detail.userInfo)
			// 如果已授权直接跳转
			if (wx.getStorageSync('authorization')) {
				wx.navigateTo({
					url: '../user_center/index/index',
				})
				return
			}

			var urls = app.globalData.SERVER_IP + "/user/update_user"
			var parsams = {}
			parsams.u_id = wx.getStorageSync('userInfo').userid
			parsams.u_header_url = e.detail.userInfo.avatarUrl
			parsams.u_nickname = e.detail.userInfo.nickName

			console.log(parsams)

			http.P(urls, parsams, (res) => {
				console.log(res.result.success)
				if (res.result.success) {
					wx.setStorageSync('authorization', true)
					wx.navigateTo({
						url: '../user_center/index/index',
					})
				}
			}, (err) => {
				console.log("获取信息失败")
				console.log(err)
			})

		} else {
			console.log("----授权失败")
			console.log(e)
			wx.navigateTo({
				url: '../user_center/index/index',
			})
		}
	},
	// 用户中心
	userCenter: function (e) {
		wx.navigateTo({
			url: '../user_center/index/index',
		})
	},
	// 课程点击
	courseBtn: function (e) {
		console.log(e.currentTarget.dataset.id)
		wx.navigateTo({
			url: '../activity/course/course?activityId=' + e.currentTarget.dataset.id + '&o_id=692',
		})
	},

	// 热门活动点击
	activityBtn: function (e) {
		console.log(e.currentTarget.dataset.id)
		wx.navigateTo({
			url: '../activity/promotion/promotion?activityId=' + e.currentTarget.dataset.id + '&o_id=692',
		})
	},
	getTabData: function () {
		var that = this
		var urls = ''
		if (this.data.menuIndex == 1) {
			urls = app.globalData.SERVER_IP + "/organization/organization_characteristic_course/sort_order/oc_order-asc/o_id/692/skip/0/limit/100"
		} else if (this.data.menuIndex == 2) {
			urls = app.globalData.SERVER_IP + "/organization/organization_heat_activity/o_id/692/skip/0/limit/100"
		} else if (this.data.menuIndex == 3) {
			urls = app.globalData.SERVER_IP + "/organization/organization_comment/o_id/692/skip/0/limit/100"
		}
		let query = wx.createSelectorQuery();
		// 获取数据
		http.G(urls, {}, (res) => {
			console.log("获取TAB下数据", res.result)
			if (that.data.menuIndex == 1) {
				that.setData({
					courseData: res.result.organizationCourseData
				})

				query.select('.organization_course').boundingClientRect((rect) => {
					console.log(rect)
					let h = rect.height;
					this.setData({
						menuItemContentH: h
					})
					this.setData({
						organizationCourseH: h
					})
				}).exec();

			} else if (that.data.menuIndex == 2) {
				that.setData({
					activityData: res.result.organizationActivityData
				})

				query.select('.organization_activity').boundingClientRect((rect) => {
					console.log(rect)
					let h = rect.height;
					this.setData({
						menuItemContentH: h
					})
					this.setData({
						organizationActivityH: h
					})
				}).exec();

			} else if (that.data.menuIndex == 3) {
				that.setData({
					commontData: res.result.organizationCommentData
				})

				query.select('.organization_comment').boundingClientRect((rect) => {
					console.log(rect)
					let h = rect.height;
					this.setData({
						menuItemContentH: h
					})
					this.setData({
						organizationCommentH: h
					})
				}).exec();

			}
		}, (err) => {
			console.log("获取TAB下数据失败")
			console.log(err)
		})

	},
	getData: function () {
		var that = this
		// 获取首页数据
		var urls = app.globalData.SERVER_IP + '/organization/index/o_id/692/u_id/' + wx.getStorageSync('userInfo').userid
		http.G(urls, {}, (res) => {
			console.log("获取首页数据", res.result)
			that.setData({
				organizationData: res.result.organizationData[0]
			})
			that.setData({
				organizationDiscountData: res.result.organizationDiscountData[0]
			})
			that.setData({
				bannerData: res.result.organizationBannerData
			})
			that.setData({
				organizationDesc: common.richContentFormat(common.escape2Html(res.result.organizationData[0].o_desc))
			})
			let query = wx.createSelectorQuery();
			query.select('.organization_desc').boundingClientRect((rect) => {
				let h = rect.height * 1.7;
				this.setData({
					menuItemContentH: h
				})
				this.setData({
					organizationDescH: h
				})
			}).exec();
		}, (err) => {
			console.log("获取首页数据失败")
			console.log(err)
		})


	},
	onShow: function () {
		wx.showShareMenu({})

		setTimeout(() => {
			this.setData({
				state: 'close-view'
			})
			this.setData({
				maskStatus: false
			})
		}, 3000)

		console.log(3333);
		let query = wx.createSelectorQuery();

		query.select('.organization_service_content_box').boundingClientRect((rect) => {
			this.setData({
				menuItemContentW: rect.width
			})
		}).exec();

		query.select('.organization_service_menu_item').boundingClientRect((rect) => {
			this.setData({
				menuItemW: rect.width
			})
		}).exec();

	},
	onLoad: function () {

		//页面初始化
		this.getData()

	},
	organizationServiceMenuClick: function (event) {

		console.log(123121);
		this.setData({
			menuIndex: event.currentTarget.dataset.menuIndex
		});

		let index = event.currentTarget.dataset.menuIndex;
		this.setData({
			organizationServiceContentItemIndex: index
		});

		let query = wx.createSelectorQuery();

		if (index == 0) {
			console.log("---机构状况---")

			this.setData({
				menuItemContentH: this.data.organizationDescH
			})
		} else if (index == 1) {
			console.log("---特色课程---")
			this.setData({
				menuIndex: event.currentTarget.dataset.menuIndex
			});
			// this.getTabData()

		} else if (index == 2) {
			console.log("---热门活动---")
			this.setData({
				menuIndex: event.currentTarget.dataset.menuIndex
			});
			// this.getTabData()

		} else if (index == 3) {
			console.log("---用户评价---")
			this.setData({
				menuIndex: event.currentTarget.dataset.menuIndex
			});
			// this.getTabData()

		}

	},
	swiperBindChange(event) {
		this.setData({
			organizationServiceContentItemIndex: event.detail.current,
			lastMenuItemSelectedX: this.data.menuItemSelectedX
		})

		this.setData({
			menuIndex: event.detail.current
		});
		if (event.detail.current != 0) {
			this.getTabData()
		}
	},
	swiperBindTransition(event) {
		let transitionX = this.data.lastMenuItemSelectedX + (event.detail.dx) / 4
		this.setData({
			menuItemSelectedX: transitionX
		})
	},
	getScore() {

	},
	moreDataClick(event) {
		console.log(event);
		let type = event.currentTarget.dataset['moretype'];

		//获取更多数据
		this.setData({
			"courseData": this.data.courseData.concat(this.data.courseData)
		})
		let query = wx.createSelectorQuery();
		query.select('.organization_course').boundingClientRect((rect) => {
			console.log(rect)
			let h = rect.height;
			this.setData({
				menuItemContentH: h
			})
			this.setData({
				organizationCourseH: h
			})
		}).exec();

		console.log(this.data.courseData)
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function (options) {
		var that = this;
		// 设置菜单中的转发按钮触发转发事件时的转发内容
		var shareObj = {
			title: this.data.organizationData.o_name, // 默认是小程序的名称(可以写slogan等)
			path: '', // 默认是当前页面，必须是以‘/’开头的完整路径
			imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
			success: function (res) {
				// 转发成功之后的回调
				if (res.errMsg == 'shareAppMessage:ok') {}
			},
			fail: function () {
				// 转发失败之后的回调
				if (res.errMsg == 'shareAppMessage:fail cancel') {
					// 用户取消转发
				} else if (res.errMsg == 'shareAppMessage:fail') {
					// 转发失败，其中 detail message 为详细失败信息
				}
			}
		}
		// 返回shareObj
		return shareObj;
	}
})